<?php

/**
 * trims a string to a certain length, returns escaped
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version   SVN: $Id: Truncate.php 22 2009-05-08 17:08:41Z monzee $
 */
class Crud_View_Helper_Truncate extends Zend_View_Helper_Abstract
{

    /**
     * Returns an escaped string $str truncated to length $len
     *
     * @param string $str
     * @param int    $len
     * @return string
     */
    public function truncate($str, $len = 50)
    {
        if (null === $str || '' == $str) {
            return '<em>NULL</em>';
        }
        
        if (strlen($str) > $len) {
            $str = rtrim(substr($str, 0, $len));
            return $this->view->escape($str) . '&hellip;';
        }

        return $this->view->escape($str);
    }
    
}
